﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PGMapper.Tests
{
    class Program
    {
        /*class Blog
        {
            public int BlogID { get; set; }
            public string BlogName { get; set; }
        }*/

        static void Main(string[] args)
        {
            var db = new PostgresDatabase("server=192.168.104.1;port=5432;database=mysite;user=nick");

            //var name = db.ExecuteScalar("select blog_name from blogs where blog_id = ?", 1);

            //Console.WriteLine(name);
            //> "Deep Thoughts"

            //List<string> titles = db.ExecuteList<string>("select post_title from posts");

            //Console.WriteLine(titles[1]);
            //> "I'm being watched"

            //List<Blog> rants = db.ExecuteList<Blog>("select * from blogs where blog_name ~~ ?", "%Rants%");

            //Console.WriteLine(rants[0].BlogName);
            //> "Mostly Rants"

            // Lookup an existing blog by ID
            //Blog blog = Blog.Find(1);

            //Console.WriteLine(blog.Name);
            //> "Deep Thoughts"

            // Find all blogs using just a WHERE clause
            //List<Blog> rants = Blog.FindAllWhere("blog_name ~~ ?", "%Rants%");

            //Console.WriteLine(rants[0].Name);
            //> "Mostly Rants"

            // Create a new blog
            //Blog coding = Blog.Create();
            //coding.Name = "Adventures in Coding";
            //coding.Save(); // it's in the database now

            // Delete it
            //coding.Delete();


            //Post creepy = Post.FindWhere("post_body ~~ ?", "%creepy%");

            //Console.WriteLine(creepy.Blog);
            //> "<Reference to Blog:1>"

            // this goes out to the database and fetches the Blog object
            //Blog blog = creepy.Blog.LookupTarget();

            //Console.WriteLine(blog.Name);
            //> "Deep Thoughts"

            
            //Post flowers = Post.Find(1);
            //flowers.Title = "I know what flowers are now!";

            db.PrintSqlToConsole = true;

            //flowers.Save();
            //> "update posts set post_title = 'I know what flowers are now!' where post_id = 1"


            db.MigrateFromClasses();
            //> "Adding column posts.date_posted::DateTime"
            //> "alter table posts add column date_posted timestamp without time zone"

            Post flowers = Post.Find(1);
            flowers.DatePosted = DateTime.Now;
            flowers.Save();
            //> "update posts set date_posted = '7/29/2009 5:38:11 PM' where post_id = 1"

            Console.ReadLine();
            return;
            
            /*db.MigrateFromClasses();

            Blog.DeleteAll();
            var deepThoughts = Blog.Create();
            deepThoughts.Name = "Deep Thoughts";
            deepThoughts.Save();

            var mostlyRants = Blog.Create();
            mostlyRants.Name = "Mostly Rants";
            mostlyRants.Save();

            var post1 = Post.Create();
            post1.Blog = deepThoughts;
            post1.Title = "What are flowers, really?";
            post1.Body = "Can they be trusted? I don't…";
            post1.Save();

            var post2 = Post.Create();
            post2.Blog = deepThoughts;
            post2.Title = "I'm being watched";
            post2.Body = "I get a creepy feeling that…";
            post2.Save();*/
        }
    }

    [Table("blogs")]
    public class Blog : Mapped<Blog>, IReference
    {
        [Column("blog_id", IsIdentity = true)]
        public virtual int ID { get; set; }
        [Column("blog_name")]
        public virtual string Name { get; set; }
    }

    [Table("posts")]
    public class Post : Mapped<Post>
    {
        [Column("post_id", IsIdentity = true)]
        public virtual int ID { get; set; }
        [Column("blog_id")]
        public virtual Reference<Blog> Blog { get; set; }
        [Column("post_title")]
        public virtual string Title { get; set; }
        [Column("post_body")]
        public virtual string Body { get; set; }
        [Column("date_posted")]
        public virtual DateTime DatePosted { get; set; }
    }
}
